package com.sinothk.gpserver.connection.websocket.utils;

import com.sinothk.gpserver.connection.websocket.bean.Player;
import com.sinothk.gpserver.connection.websocket.bean.RoomIM;
import com.sinothk.gpserver.data.WsConstant;
import com.sinothk.gpserver.modules.biz.service.IGroupMemberService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 编写 Liang Y.T.
 * 时间 2021/8/10 0:32
 */
@Slf4j
public class RoomTool {

    /**
     * // 将已准备人加入到玩家集合
     *
     * @param roomIM
     * @return
     */
    public static RoomIM createPlayers(RoomIM roomIM) {
        // 首先将准备人加入玩家集合
        ConcurrentHashMap<String, Player> preparerMap = roomIM.getPlayers();
        for (Player preparer : preparerMap.values()) {
            if (preparer.getRunState() == WsConstant.PLAYER_RUN.preparer) {
                roomIM.getPlayers().get(preparer.getSeatNum()).setRunState(WsConstant.PLAYER_RUN.player);
            }
        }
        setNoSpeak(roomIM);
        return roomIM;
    }

    public static void setNoSpeak(RoomIM roomIM) {
        for (Player player : roomIM.getPlayers().values()) {
            if (player.getRunState() != WsConstant.PLAYER_RUN.empty && player.getOptState() != WsConstant.PLAYER_OPT.STATE_CANCEL && player.getRunState() != WsConstant.PLAYER_OPT.STATE_ALLIN) {
                player.setHasSpeak(false);
                roomIM.getPlayers().put(player.getSeatNum(), player);
            }
        }
    }

    public static void setNoPreCast(RoomIM roomIM) {
        roomIM.setRoomCurrCast(0);

        for (Player player : roomIM.getPlayers().values()) {
            if (player.getRunState() != WsConstant.PLAYER_RUN.empty && player.getOptState() != WsConstant.PLAYER_OPT.STATE_CANCEL && player.getRunState() != WsConstant.PLAYER_OPT.STATE_ALLIN) {
                player.setPreCast(0);
                roomIM.getPlayers().put(player.getSeatNum(), player);
            }
        }
    }

    public static void addNullPlayer(RoomIM roomIM) {
        ConcurrentHashMap<String, Player> playerMap = roomIM.getPlayers();

        // 然后, 逐个判断个位置情况，无人则
        Player pA = playerMap.get(WsConstant.ROOM_SEAT.SEAT_ROOM_A);
        if (pA == null) {
            Player playerEmpty = new Player();
            playerEmpty.setSeatNum("A");
            playerEmpty.setRunState(WsConstant.PLAYER_RUN.empty);
            roomIM.getPlayers().put(WsConstant.ROOM_SEAT.SEAT_ROOM_A, playerEmpty);
        }

        Player pB = playerMap.get(WsConstant.ROOM_SEAT.SEAT_ROOM_B);
        if (pB == null) {
            Player playerEmpty = new Player();
            playerEmpty.setSeatNum("B");
            playerEmpty.setRunState(WsConstant.PLAYER_RUN.empty);
            roomIM.getPlayers().put(WsConstant.ROOM_SEAT.SEAT_ROOM_B, playerEmpty);
        }

        Player pC = playerMap.get(WsConstant.ROOM_SEAT.SEAT_ROOM_C);
        if (pC == null) {
            Player playerEmpty = new Player();
            playerEmpty.setSeatNum("C");
            playerEmpty.setRunState(WsConstant.PLAYER_RUN.empty);
            roomIM.getPlayers().put(WsConstant.ROOM_SEAT.SEAT_ROOM_C, playerEmpty);
        }

        Player pD = playerMap.get(WsConstant.ROOM_SEAT.SEAT_ROOM_D);
        if (pD == null) {
            Player playerEmpty = new Player();
            playerEmpty.setSeatNum("D");
            playerEmpty.setRunState(WsConstant.PLAYER_RUN.empty);
            roomIM.getPlayers().put(WsConstant.ROOM_SEAT.SEAT_ROOM_D, playerEmpty);
        }

        Player pE = playerMap.get(WsConstant.ROOM_SEAT.SEAT_ROOM_E);
        if (pE == null) {
            Player playerEmpty = new Player();
            playerEmpty.setSeatNum("E");
            playerEmpty.setRunState(WsConstant.PLAYER_RUN.empty);
            roomIM.getPlayers().put(WsConstant.ROOM_SEAT.SEAT_ROOM_E, playerEmpty);
        }

        if (roomIM.getRoomType() == 0) { // 不等 2 五张模式 进入
            Player pF = playerMap.get(WsConstant.ROOM_SEAT.SEAT_ROOM_F);
            if (pF == null) {
                Player playerEmpty = new Player();
                playerEmpty.setSeatNum("F");
                playerEmpty.setRunState(WsConstant.PLAYER_RUN.empty);
                roomIM.getPlayers().put(WsConstant.ROOM_SEAT.SEAT_ROOM_F, playerEmpty);
            }

            Player pG = playerMap.get(WsConstant.ROOM_SEAT.SEAT_ROOM_G);
            if (pG == null) {
                Player playerEmpty = new Player();
                playerEmpty.setSeatNum("G");
                playerEmpty.setRunState(WsConstant.PLAYER_RUN.empty);
                roomIM.getPlayers().put(WsConstant.ROOM_SEAT.SEAT_ROOM_G, playerEmpty);
            }

            Player pH = playerMap.get(WsConstant.ROOM_SEAT.SEAT_ROOM_H);
            if (pH == null) {
                Player playerEmpty = new Player();
                playerEmpty.setSeatNum("H");
                playerEmpty.setRunState(WsConstant.PLAYER_RUN.empty);
                roomIM.getPlayers().put(WsConstant.ROOM_SEAT.SEAT_ROOM_H, playerEmpty);
            }

            Player pI = playerMap.get(WsConstant.ROOM_SEAT.SEAT_ROOM_I);
            if (pI == null) {
                Player playerEmpty = new Player();
                playerEmpty.setSeatNum("I");
                playerEmpty.setRunState(WsConstant.PLAYER_RUN.empty);
                roomIM.getPlayers().put(WsConstant.ROOM_SEAT.SEAT_ROOM_I, playerEmpty);
            }
        }
    }

    /**
     * 找出并更新现有玩家
     *
     * @param roomIM
     */
    public static void refreshCanSpeakPlayerSeat(RoomIM roomIM) {
        ConcurrentHashMap<String, Player> playersMap = roomIM.getPlayers();

        String enableAskSeats = "";

        for (Player player : playersMap.values()) {
            if (player.getRunState() != WsConstant.PLAYER_RUN.preparer && // 不是准备人
                    player.getRunState() != WsConstant.PLAYER_RUN.empty && // 不是空用户
                    player.getOptState() != WsConstant.PLAYER_OPT.STATE_CANCEL && // 不是弃牌人
                    player.getRunState() != WsConstant.PLAYER_OPT.STATE_ALLIN) { // 不是ALLIN人

                if (StringUtils.isEmpty(enableAskSeats)) {// 第一个则不加#
                    enableAskSeats = player.getSeatNum();
                } else {
                    enableAskSeats = enableAskSeats + "#" + player.getSeatNum();
                }
            }
        }
        roomIM.setEnableAskSeats(enableAskSeats);
    }

    public static void startRecycle(RoomIM roomIMPlaying) {
        // A B C D E
        String[] enableSeatArr = roomIMPlaying.getEnableAskSeats().trim().split("#");

        String minCastNo = roomIMPlaying.getMinCastNo();// 小盲位置

        // 将目前的小盲付给大盲
        roomIMPlaying.setBankerNo(minCastNo);
        // 设置
        roomIMPlaying.getPlayers().get(minCastNo).setPlayState(WsConstant.PLAYER_PLAY.WAITING);

        // 通过计算找到下一个小盲
        for (int i = 0; i < enableSeatArr.length; i++) {

            if (enableSeatArr[i].equals(minCastNo)) {
                int nextIndex = i + 1;
                String seatNum;
                if (nextIndex < enableSeatArr.length) { // 后面有人，则加1取出
                    seatNum = enableSeatArr[i + 1];
                } else { // 后面没人，则取出第一个
                    seatNum = enableSeatArr[0];
                }
                roomIMPlaying.setMinCastNo(seatNum);
                roomIMPlaying.getPlayers().get(seatNum).setPlayState(WsConstant.PLAYER_PLAY.SPEAKING);
            }
        }
    }

    public static void autoRecycle(RoomIM roomIMPlaying) {
        // A B C D E
        String[] enableSeatArr = roomIMPlaying.getEnableAskSeats().trim().split("#");

        String minCastNo = roomIMPlaying.getMinCastNo();// 小盲位置
        // 将目前的小盲付给大盲
        roomIMPlaying.setBankerNo(minCastNo);


        // 设置
        roomIMPlaying.getPlayers().get(minCastNo).setPlayState(WsConstant.PLAYER_PLAY.WAITING);

        // 通过计算找到下一个小盲
        for (int i = 0; i < enableSeatArr.length; i++) {

            if (enableSeatArr[i].equals(minCastNo)) {
                int nextIndex = i + 1;
                String seatNum;

                if (nextIndex < enableSeatArr.length) { // 后面有人，则加1取出
                    seatNum = enableSeatArr[i + 1];

                } else { // 后面没人，则取出第一个
                    seatNum = enableSeatArr[0];
                }

                roomIMPlaying.setMinCastNo(seatNum);
                roomIMPlaying.getPlayers().get(seatNum).setPlayState(WsConstant.PLAYER_PLAY.SPEAKING);
            }
        }
    }

    public static void initBaseInfo(RoomIM roomIM, IGroupMemberService groupMemberService) {
        Player bankerPlayer = roomIM.getPlayers().get(roomIM.getBankerNo());// 大盲 庄家位置
        Player minPlayer = roomIM.getPlayers().get(roomIM.getMinCastNo()); // 小盲

        // 扣除大盲玩家积分
        float bankerPlayerCoins = bankerPlayer.getOwnCoins() - roomIM.getBigCast();
        bankerPlayer.setOwnCoins(bankerPlayerCoins);
        // 更新大盲用户积分
        groupMemberService.updateUserCoins(roomIM.getRoomNum(), bankerPlayer.getAccount(), bankerPlayerCoins);
        bankerPlayer.setOwnCoins(bankerPlayerCoins);

        // 扣除小盲玩家积分
        float minPlayerCoins = minPlayer.getOwnCoins() - roomIM.getMinCast();
        minPlayer.setOwnCoins(minPlayerCoins);
        // 更新小盲用户积分
        groupMemberService.updateUserCoins(roomIM.getRoomNum(), minPlayer.getAccount(), minPlayerCoins);

        // 设置 池底 = 大盲扣除 + 小盲扣除
        roomIM.setBaseCoins(roomIM.getBigCast() + roomIM.getMinCast());

        // 设置此刻跟注值: 此时 既是 大盲值
        roomIM.setRoomCurrCast(roomIM.getBigCast());

        // 设置大盲用户总投注
        bankerPlayer.setPreCast(roomIM.getBigCast());
        float preAllCastBanker = bankerPlayer.getPreAllCast();
        bankerPlayer.setPreAllCast(preAllCastBanker + roomIM.getBigCast());

        // 设置小盲用户总投注
        minPlayer.setPreCast(roomIM.getMinCast());
        float preAllCastMin = minPlayer.getPreAllCast();
        minPlayer.setPreAllCast(preAllCastMin + roomIM.getMinCast());
    }

    public static void initBaseInfoFor5Cards(RoomIM roomIM, IGroupMemberService groupMemberService) {
        float bigCast = roomIM.getBigCast();
        for (Player player : roomIM.getPlayers().values()) {
            if (player.getRunState() == WsConstant.PLAYER_RUN.player) {
                player.setPreCast(bigCast);// 本轮投注
                player.setPreAllCast(bigCast);// 本局投注

                // 扣除每个玩家的底分,得到每个人的剩余积分
                float playerRemainingCoins = player.getOwnCoins() - bigCast;
                // 更新内存
                player.setOwnCoins(playerRemainingCoins);
                // 更新表
                groupMemberService.updateUserCoins(roomIM.getRoomNum(), player.getAccount(), playerRemainingCoins);

                // 累加得到第一轮池底
                roomIM.setBaseCoins(roomIM.getBaseCoins() + bigCast);
            }
        }
        roomIM.setRoomCurrCast(bigCast);


//        Player bankerPlayer = roomIM.getPlayers().get(roomIM.getBankerNo());// 大盲 庄家位置
//        Player minPlayer = roomIM.getPlayers().get(roomIM.getMinCastNo()); // 小盲
//
//        // 扣除大盲玩家积分
//        float bankerPlayerCoins = bankerPlayer.getOwnCoins() - roomIM.getBigCast();
//        bankerPlayer.setOwnCoins(bankerPlayerCoins);
//        // 更新大盲用户积分
//        groupMemberService.updateUserCoins(roomIM.getRoomNum(), bankerPlayer.getAccount(), bankerPlayerCoins);
//        bankerPlayer.setOwnCoins(bankerPlayerCoins);
//
//        // 扣除小盲玩家积分
//        float minPlayerCoins = minPlayer.getOwnCoins() - roomIM.getMinCast();
//        minPlayer.setOwnCoins(minPlayerCoins);
//        // 更新小盲用户积分
//        groupMemberService.updateUserCoins(roomIM.getRoomNum(), minPlayer.getAccount(), minPlayerCoins);
//
//        // 设置 池底 = 大盲扣除 + 小盲扣除
//        roomIM.setBaseCoins(roomIM.getBigCast() + roomIM.getMinCast());
//
//        // 设置此刻跟注值: 此时 既是 大盲值
//        roomIM.setRoomCurrCast(roomIM.getBigCast());
//
//        // 设置大盲用户总投注
//        bankerPlayer.setPreCast(roomIM.getBigCast());
//        float preAllCastBanker = bankerPlayer.getPreAllCast();
//        bankerPlayer.setPreAllCast(preAllCastBanker + roomIM.getBigCast());
//
//        // 设置小盲用户总投注
//        minPlayer.setPreCast(roomIM.getMinCast());
//        float preAllCastMin = minPlayer.getPreAllCast();
//        minPlayer.setPreAllCast(preAllCastMin + roomIM.getMinCast());
    }

    public static RoomIM getBackRoomByRoom(RoomIM roomIM) {
        RoomIM newPokersRoom = new RoomIM();
        BeanUtils.copyProperties(roomIM, newPokersRoom);
//        noPubPokersRoom.setTexasPoker(null);// 清空发牌信息

        return newPokersRoom;
    }

    public static void resetRoom(RoomIM roomIM) {
        roomIM.setWinner("");// 清空胜者
        roomIM.setHasSendPubCard(false); // 设置为未发牌
        roomIM.setTexasPoker(null);
        roomIM.setBaseCoins(0);

        RoomTool.setNoSpeak(roomIM);

        for (Player player : roomIM.getPlayers().values()) {
            if (player.getRunState() != WsConstant.PLAYER_RUN.empty) {
                player.setPreCast(0);
                player.setPreAllCast(0);
                player.setOptState(WsConstant.PLAYER_OPT.STATE_INIT);
                player.setUserPokers(null);
                roomIM.getPlayers().put(player.getSeatNum(), player);
            }
        }
    }

    public static ArrayList<Player> getLivePlayers(RoomIM roomIM) {
        ArrayList<Player> livePlayers = new ArrayList<>();
        for (Player player : roomIM.getPlayers().values()) {
            if (player.getRunState() != WsConstant.PLAYER_RUN.empty && player.getOptState() != WsConstant.PLAYER_OPT.STATE_CANCEL) {
                livePlayers.add(player);
            }
        }
        return livePlayers;
    }

    /**
     * 全张结算
     *
     * @param roomIM
     * @param groupMemberService
     */
    public static void setCompareResult(RoomIM roomIM, IGroupMemberService groupMemberService) {

        Player winner = null;

        for (Player player : roomIM.getPlayers().values()) {
            if (player.getRunState() != WsConstant.PLAYER_RUN.empty
                    && player.getOptState() != WsConstant.PLAYER_OPT.STATE_CANCEL
                    && StringUtils.isNotEmpty(roomIM.getWinner()) && roomIM.getWinner().contains(player.getAccount())) {
                winner = player;
            }
        }

        if (winner == null) {
            log.info("找不到赢家");
            return;
        }

        for (Player player : roomIM.getPlayers().values()) {

            if (player.getRunState() != WsConstant.PLAYER_RUN.empty && player.getOptState() != WsConstant.PLAYER_OPT.STATE_CANCEL) {

                if (StringUtils.isNotEmpty(roomIM.getWinner()) && roomIM.getWinner().contains(player.getAccount())) {
                    // 赢家
                    player.setWinner(true);

                    if (player.optState != WsConstant.PLAYER_OPT.STATE_ALLIN) {
                        float baseCoins = roomIM.getBaseCoins();
                        float userAllCast = player.getPreAllCast();

                        // 如果是 正常流程用户 赢
                        // 赢数  =  池底 - 赢家投入；
                        float winCast = baseCoins - userAllCast;

                        // 赢家赢得数
//                        int winnerPc = 100 - roomIM.getRoomLeaderPc();
//                        float winCastUser = winCast * winnerPc / 100f; // 赢家实际赢得数
                        float winCastUser = winCast;
                        // 增加赢家的积分
                        player.setPreWinCast(winCastUser);
                        player.setOwnCoins(player.getOwnCoins() + userAllCast + winCastUser);// 更新内存用户积分
                        groupMemberService.addUserCoins(roomIM.getRoomNum(), player.getAccount(), userAllCast + winCastUser);

//                    // 处理房主积分
//                    float winCastAdmin = winCast * roomIM.getRoomLeaderPc() / 100f;// 房主分成数
//                    groupMemberService.addUserCoins(roomIM.getRoomNum(), roomIM.getRoomLeaderAccount(), winCastAdmin);
//                    // 更新房主内存积分数
//                    for (Player leader : roomIM.getPlayers().values()) {
//                        if (StringUtils.isNotEmpty(leader.getAccount()) && leader.getAccount().equals(roomIM.getRoomLeaderAccount())) {
//                            leader.setOwnCoins(leader.getOwnCoins() + winCastAdmin);
//                        }
//                    }
//
//
//                    // 处理合伙人积分
//                    if (StringUtils.isNotEmpty(roomIM.getPartnerAccount()) && roomIM.getPartnerPc() > 0) {// 如果有合伙人，则分成
//                        float winCastPartner = winCastAdmin * roomIM.getPartnerPc() / 100f;// 合伙人分成数
//                        groupMemberService.addUserCoins(roomIM.getRoomNum(), roomIM.getPartnerAccount(), winCastPartner);
//                        // 更新合伙人内存积分数
//                        for (Player partner : roomIM.getPlayers().values()) {
//                            if (StringUtils.isNotEmpty(partner.getAccount()) && partner.getAccount().equals(roomIM.getPartnerAccount())) {
//                                partner.setOwnCoins(partner.getOwnCoins() + winCastPartner);
//                            }
//                        }
//                    }

                    } else {
                        // 如果是 AllIn用户 赢
                        for (Player player2 : roomIM.getPlayers().values()) {// 赢家Allin后，各位玩家继续投注的积分退还给各位
                            if (player2.getRunState() != WsConstant.PLAYER_RUN.empty && player2.getOptState() != WsConstant.PLAYER_OPT.STATE_CANCEL
                                    && !player.getAccount().equals(player2.getAccount())) {

                                float differenceValue = player2.getPreAllCast() - player.getPreAllCast();
                                if (differenceValue > 0) {
                                    // 更细用户值
                                    float nowOwnCoins = player2.getOwnCoins() + differenceValue;
                                    player2.setOwnCoins(nowOwnCoins);
                                    groupMemberService.addUserCoins(roomIM.getRoomNum(), player2.getAccount(), nowOwnCoins);
                                    // 设置剩余低分
                                    roomIM.setBaseCoins(roomIM.getBaseCoins() - differenceValue);

                                    roomIM.setRoomCurrCast(player.getPreAllCast());
                                }
                            }
                        }

                        float baseCoins = roomIM.getBaseCoins();
                        float userAllCast = player.getPreAllCast();

                        // 如果是 正常流程用户 赢
                        // 赢数  =  池底 - 赢家投入；
                        float winCast = baseCoins - userAllCast;

                        // 赢家赢得数
//                        int winnerPc = 100 - roomIM.getRoomLeaderPc();
//                        float winCastUser = winCast * winnerPc / 100f; // 赢家实际赢得数
                        float winCastUser = winCast;
                        // 增加赢家的积分
                        player.setPreWinCast(winCastUser);
                        player.setOwnCoins(player.getOwnCoins() + userAllCast + winCastUser);// 更新内存用户积分
                        groupMemberService.addUserCoins(roomIM.getRoomNum(), player.getAccount(), userAllCast + winCastUser);

//                    // 处理房主积分
//                    float winCastAdmin = winCast * roomIM.getRoomLeaderPc() / 100f;// 房主分成数
//                    groupMemberService.addUserCoins(roomIM.getRoomNum(), roomIM.getRoomLeaderAccount(), winCastAdmin);
//                    // 更新房主内存积分数
//                    for (Player leader : roomIM.getPlayers().values()) {
//                        if (StringUtils.isNotEmpty(leader.getAccount()) && leader.getAccount().equals(roomIM.getRoomLeaderAccount())) {
//                            leader.setOwnCoins(leader.getOwnCoins() + winCastAdmin);
//                        }
//                    }
//
//
//                    // 处理合伙人积分
//                    if (StringUtils.isNotEmpty(roomIM.getPartnerAccount()) && roomIM.getPartnerPc() > 0) {// 如果有合伙人，则分成
//                        float winCastPartner = winCastAdmin * roomIM.getPartnerPc() / 100f;// 合伙人分成数
//                        groupMemberService.addUserCoins(roomIM.getRoomNum(), roomIM.getPartnerAccount(), winCastPartner);
//                        // 更新合伙人内存积分数
//                        for (Player partner : roomIM.getPlayers().values()) {
//                            if (StringUtils.isNotEmpty(partner.getAccount()) && partner.getAccount().equals(roomIM.getPartnerAccount())) {
//                                partner.setOwnCoins(partner.getOwnCoins() + winCastPartner);
//                            }
//                        }
//                    }

                    }
                } else {
                    // 输家
                    player.setWinner(false);

                    float d3Value = winner.getPreAllCast() - player.getPreAllCast();
                    float thisLostCast;
                    if (d3Value >= 0) {
                        // 如果实际投入小于本局所输, 则全部投入全输
                        thisLostCast = player.getPreAllCast() * -1;
                    } else {
                        // 如果实际投入大于本局所输, 则输掉部分，既是本局的整体所输
                        thisLostCast = winner.getPreAllCast() * -1;
                    }
                    player.setPreWinCast(thisLostCast);
                }
            } else {
                if (player.getOptState() == WsConstant.PLAYER_OPT.STATE_CANCEL) {// 弃牌用户
                    player.setWinner(false);
                    float preAllCast = player.getPreAllCast() * -1;
                    player.setPreWinCast(preAllCast);
                }
            }

            roomIM.getPlayers().put(player.getSeatNum(), player);
        }

    }

    /**
     * 5张结算
     *
     * @param roomIM
     * @param groupMemberService
     */
    public static void setCompareResultFor5Cards(RoomIM roomIM, IGroupMemberService groupMemberService) {

        Player winner = null;

        for (Player player : roomIM.getPlayers().values()) {
            if (player.getRunState() != WsConstant.PLAYER_RUN.empty
                    && player.getOptState() != WsConstant.PLAYER_OPT.STATE_CANCEL
                    && StringUtils.isNotEmpty(roomIM.getWinner()) && roomIM.getWinner().contains(player.getAccount())) {
                winner = player;
            }
        }

        if (winner == null) {
            log.info("找不到赢家");
            return;
        }

        for (Player player : roomIM.getPlayers().values()) {

            if (player.getRunState() != WsConstant.PLAYER_RUN.empty && player.getOptState() != WsConstant.PLAYER_OPT.STATE_CANCEL) {

                if (StringUtils.isNotEmpty(roomIM.getWinner()) && roomIM.getWinner().contains(player.getAccount())) {
                    // 赢家
                    player.setWinner(true);

                    if (player.optState != WsConstant.PLAYER_OPT.STATE_ALLIN) {
                        float baseCoins = roomIM.getBaseCoins();
                        float userAllCast = player.getPreAllCast();

                        // 如果是 正常流程用户 赢
                        // 赢数  =  池底 - 赢家投入；
                        float winCast = baseCoins - userAllCast;

                        // 赢家赢得数
//                        int winnerPc = 100 - roomIM.getRoomLeaderPc();
//                        float winCastUser = winCast * winnerPc / 100f; // 赢家实际赢得数
                        float winCastUser = winCast;
                        // 增加赢家的积分
                        player.setPreWinCast(winCastUser);
                        player.setOwnCoins(player.getOwnCoins() + userAllCast + winCastUser);// 更新内存用户积分
                        groupMemberService.addUserCoins(roomIM.getRoomNum(), player.getAccount(), userAllCast + winCastUser);

//                    // 处理房主积分
//                    float winCastAdmin = winCast * roomIM.getRoomLeaderPc() / 100f;// 房主分成数
//                    groupMemberService.addUserCoins(roomIM.getRoomNum(), roomIM.getRoomLeaderAccount(), winCastAdmin);
//                    // 更新房主内存积分数
//                    for (Player leader : roomIM.getPlayers().values()) {
//                        if (StringUtils.isNotEmpty(leader.getAccount()) && leader.getAccount().equals(roomIM.getRoomLeaderAccount())) {
//                            leader.setOwnCoins(leader.getOwnCoins() + winCastAdmin);
//                        }
//                    }
//
//
//                    // 处理合伙人积分
//                    if (StringUtils.isNotEmpty(roomIM.getPartnerAccount()) && roomIM.getPartnerPc() > 0) {// 如果有合伙人，则分成
//                        float winCastPartner = winCastAdmin * roomIM.getPartnerPc() / 100f;// 合伙人分成数
//                        groupMemberService.addUserCoins(roomIM.getRoomNum(), roomIM.getPartnerAccount(), winCastPartner);
//                        // 更新合伙人内存积分数
//                        for (Player partner : roomIM.getPlayers().values()) {
//                            if (StringUtils.isNotEmpty(partner.getAccount()) && partner.getAccount().equals(roomIM.getPartnerAccount())) {
//                                partner.setOwnCoins(partner.getOwnCoins() + winCastPartner);
//                            }
//                        }
//                    }

                    } else {
                        // 如果是 AllIn用户 赢
                        for (Player player2 : roomIM.getPlayers().values()) {// 赢家Allin后，各位玩家继续投注的积分退还给各位
                            if (player2.getRunState() != WsConstant.PLAYER_RUN.empty && player2.getOptState() != WsConstant.PLAYER_OPT.STATE_CANCEL
                                    && !player.getAccount().equals(player2.getAccount())) {

                                float differenceValue = player2.getPreAllCast() - player.getPreAllCast();
                                if (differenceValue > 0) {
                                    // 更细用户值
                                    float nowOwnCoins = player2.getOwnCoins() + differenceValue;
                                    player2.setOwnCoins(nowOwnCoins);
                                    groupMemberService.addUserCoins(roomIM.getRoomNum(), player2.getAccount(), nowOwnCoins);
                                    // 设置剩余低分
                                    roomIM.setBaseCoins(roomIM.getBaseCoins() - differenceValue);

                                    roomIM.setRoomCurrCast(player.getPreAllCast());
                                }
                            }
                        }

                        float baseCoins = roomIM.getBaseCoins();
                        float userAllCast = player.getPreAllCast();

                        // 如果是 正常流程用户 赢
                        // 赢数  =  池底 - 赢家投入；
                        float winCast = baseCoins - userAllCast;

                        // 赢家赢得数
//                        int winnerPc = 100 - roomIM.getRoomLeaderPc();
//                        float winCastUser = winCast * winnerPc / 100f; // 赢家实际赢得数
                        float winCastUser = winCast;
                        // 增加赢家的积分
                        player.setPreWinCast(winCastUser);
                        player.setOwnCoins(player.getOwnCoins() + userAllCast + winCastUser);// 更新内存用户积分
                        groupMemberService.addUserCoins(roomIM.getRoomNum(), player.getAccount(), userAllCast + winCastUser);

//                    // 处理房主积分
//                    float winCastAdmin = winCast * roomIM.getRoomLeaderPc() / 100f;// 房主分成数
//                    groupMemberService.addUserCoins(roomIM.getRoomNum(), roomIM.getRoomLeaderAccount(), winCastAdmin);
//                    // 更新房主内存积分数
//                    for (Player leader : roomIM.getPlayers().values()) {
//                        if (StringUtils.isNotEmpty(leader.getAccount()) && leader.getAccount().equals(roomIM.getRoomLeaderAccount())) {
//                            leader.setOwnCoins(leader.getOwnCoins() + winCastAdmin);
//                        }
//                    }
//
//
//                    // 处理合伙人积分
//                    if (StringUtils.isNotEmpty(roomIM.getPartnerAccount()) && roomIM.getPartnerPc() > 0) {// 如果有合伙人，则分成
//                        float winCastPartner = winCastAdmin * roomIM.getPartnerPc() / 100f;// 合伙人分成数
//                        groupMemberService.addUserCoins(roomIM.getRoomNum(), roomIM.getPartnerAccount(), winCastPartner);
//                        // 更新合伙人内存积分数
//                        for (Player partner : roomIM.getPlayers().values()) {
//                            if (StringUtils.isNotEmpty(partner.getAccount()) && partner.getAccount().equals(roomIM.getPartnerAccount())) {
//                                partner.setOwnCoins(partner.getOwnCoins() + winCastPartner);
//                            }
//                        }
//                    }

                    }
                } else {
                    // 输家
                    player.setWinner(false);

                    float d3Value = winner.getPreAllCast() - player.getPreAllCast();
                    float thisLostCast;
                    if (d3Value >= 0) {
                        // 如果实际投入小于本局所输, 则全部投入全输
                        thisLostCast = player.getPreAllCast() * -1;
                    } else {
                        // 如果实际投入大于本局所输, 则输掉部分，既是本局的整体所输
                        thisLostCast = winner.getPreAllCast() * -1;
                    }
                    player.setPreWinCast(thisLostCast);
                }
            } else {
                if (player.getOptState() == WsConstant.PLAYER_OPT.STATE_CANCEL) {// 弃牌用户
                    player.setWinner(false);
                    float preAllCast = player.getPreAllCast() * -1;
                    player.setPreWinCast(preAllCast);
                }
            }

            roomIM.getPlayers().put(player.getSeatNum(), player);
        }

    }
}
